package com.jlpay.openapi.demo.utils;

import com.jlpay.open.jlpay.sdk.java.common.crypto.CryptoHelper;
import com.jlpay.open.jlpay.sdk.java.utils.gm.KeyGenerateUtils;

/**
 * @author chenjunhong
 * @date 2024/4/11
 */
public class CryptoHelperTest {

    // 模拟机构私钥
    public static final String ORG_PRIVATE_KEY = "-----BEGIN EC PRIVATE KEY-----\n" +
            "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQg6M4oLsWTFGnGnIeK\n" +
            "2xY59sOyNBrMudlRwsQYmj+T1OSgCgYIKoEcz1UBgi2hRANCAAQDI8H/QjDgMYwB\n" +
            "hAwCX4gFjDoiTmZafBOmPAYdY4rDS/JEs9sEBpWLobThPu4YoYRQ+27gjHoDIZI2\n" +
            "98/Rj3Qw\n" +
            "-----END EC PRIVATE KEY-----\n";

    // 模拟机构公钥
    public static final String ORG_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\n" +
            "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEAyPB/0Iw4DGMAYQMAl+IBYw6Ik5m\n" +
            "WnwTpjwGHWOKw0vyRLPbBAaVi6G04T7uGKGEUPtu4Ix6AyGSNvfP0Y90MA==\n" +
            "-----END PUBLIC KEY-----\n";

    // 模拟嘉联私钥
    public static final String JLPAY_PRIVATE_KEY = "-----BEGIN EC PRIVATE KEY-----\n" +
            "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQg7b2VC7sH8xRUrpRo\n" +
            "/7BImfQyFyd21fSovhZtxIsNXeWgCgYIKoEcz1UBgi2hRANCAAS6RX1zk3/yRbvO\n" +
            "672qkEF7gvt8WeUBr36AoDVWecerm0+fsI5jRAiCjxbecZinT1DnnrslAnSuZ0Ko\n" +
            "+ib5dVFF\n" +
            "-----END EC PRIVATE KEY-----\n";

    // 模拟嘉联公钥
    public static final String JLPAY_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\n" +
            "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEukV9c5N/8kW7zuu9qpBBe4L7fFnl\n" +
            "Aa9+gKA1VnnHq5tPn7COY0QIgo8W3nGYp09Q5567JQJ0rmdCqPom+XVRRQ==\n" +
            "-----END PUBLIC KEY-----\n";

    public static void main(String[] args) {
        // 模拟机构生成SM4加密密钥并进行Base64编码
        String orgKey = KeyGenerateUtils.generateSm4Key(32 * 4);

        // 模拟机构使用嘉联公钥加密对称密钥
        String encryptedOrgKey = CryptoHelper.encryptWithSm2(orgKey, JLPAY_PUBLIC_KEY);

        // 模拟机构使用对称密钥对敏感字段进行加密
        String orgPlaintext = "张三";
        String orgCiphertext = CryptoHelper.encryptWithSm4(orgPlaintext, orgKey);

        // 模拟嘉联使用嘉联私钥解密对称密钥
        String decryptedOrgKey = CryptoHelper.decryptWithSm2(encryptedOrgKey, JLPAY_PRIVATE_KEY);

        // 断言
        assert orgKey.equals(decryptedOrgKey);

        // 模拟嘉联使用对称密钥对敏感字段进行解密
        String decryptedOrgPlaintext = CryptoHelper.decryptWithSm4(orgCiphertext, decryptedOrgKey);

        // 断言
        assert orgPlaintext.equals(decryptedOrgPlaintext);

        // 模拟嘉联生成SM4加密密钥并进行Base64编码
        String jlpayKey = KeyGenerateUtils.generateSm4Key(32 * 4);

        // 模拟嘉联使用机构公钥加密对称密钥
        String encryptedJlpayKey = CryptoHelper.encryptWithSm2(jlpayKey, ORG_PUBLIC_KEY);

        // 模拟嘉联使用对称密钥对敏感字段进行加密
        String jlpayPlaintext = "李四";
        String jlpayCiphertext = CryptoHelper.encryptWithSm4(jlpayPlaintext, jlpayKey);

        // 模拟机构使用机构私钥解密对称密钥
        String decryptedJlpayKey = CryptoHelper.decryptWithSm2(encryptedJlpayKey, ORG_PRIVATE_KEY);

        // 断言
        assert jlpayKey.equals(decryptedJlpayKey);

        // 模拟机构使用对称密钥对敏感字段进行解密
        String decryptedJlpayPlaintext = CryptoHelper.decryptWithSm4(jlpayCiphertext, decryptedJlpayKey);

        // 断言
        assert jlpayPlaintext.equals(decryptedJlpayPlaintext);
    }

}
